/*pages/wxLogin/wxLogin.js*/
import httpsUtils from '../../utils/https.js';
import myUtils from '../../utils/util.js';

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loginType: 1,//loginType:跳转页面，默认1-跳转我的页面，2-跳转发布页面,3-商品详情,4-购物车添加,5-进货车,7-我的收藏，8-店铺关注,9订单页面，10跳尾货信息
    isTab: false,//是否tab 页
  },
  /*  事件函数  */
  register: function () {
    wx.navigateTo({
      url: '../register/register?backStep=2&isTab=' + this.data.isTab,
    })
  },
  msgLogin: function () {
    wx.navigateTo({
      url: '../msgLogin/msgLogin?backStep=2&isTab=' + this.data.isTab,
    })
  },

  /**
   * 开始登陆
   */
  startLogin: function (wxUserInfo, code) {
    
    var that = this;
    httpsUtils.postData({
      _url: app.globalData._url + '/user/xcxLogin',
      data: { code: code, iv: wxUserInfo.iv, encryptedData: wxUserInfo.encryptedData },
    }, function (json) {
      wx.hideLoading();
      if (json.success) {
        wx.setStorageSync('userInfo', json.data);
        wx.setStorageSync('utoken', json.data.token);
        app.globalData.token = json.data.token;

        if (that.data.loginType == 2) {
          wx.redirectTo({
            url: '../askToBuyIssue2/askToBuyIssue2?inIndex=false',
            success: function () {
              wx.showToast({
                title: '登陆成功',
              })
            }
          })
        } else if (that.data.loginType == 3) {
          app.globalData.isFlush = true;
          wx.navigateBack({
            delta: 1,
            success: function () {
              wx.showToast({
                title: '登陆成功',
              })
            },
          })
        }else if (that.data.loginType == 4 || that.data.loginType == 5 || that.data.loginType == 7 || that.data.loginType == 8) {
          wx.navigateBack({
            delta: 1,
            success: function () {
              wx.showToast({
                title: '登陆成功',
              })
            }
          })
        } else if (that.data.loginType == 10) {
          wx.redirectTo({
            url: '../askToBuyTail/askToBuyTail',
            success: function () {
              wx.showToast({
                title: '登陆成功',
              })
            }
          })
        } else if (that.data.loginType == 11) {
          wx.switchTab({
            url: '../askToBuyIssue/askToBuyIssue',
            success: function () {
              wx.showToast({
                title: '登陆成功',
              })
            }
          })
        } else if (that.data.loginType == 12) {
          wx.redirectTo({
            url: '../publishToNew/publishToNew',
            success: function () {
              wx.showToast({
                title: '登陆成功',
              })
            }
          })
        } else if (that.data.loginType == 13) {
          wx.switchTab({
            url: '../fabu/fabu',
            success: function () {
              wx.showToast({
                title: '登陆成功',
              })
            }
          })
        } else {
          wx.switchTab({
            url: '../myinfo/myinfo',
            success: function () {
              wx.showToast({
                title: '登陆成功',
              })
            }
          })
        }
      } else if (!json.success && json.code == 'UNREGISTERED') {
        wx.showModal({
          title: '提示',
          showCancel: false,
          content: '请绑定手机号',
          success: function (res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '../register/register?backStep=2&isTab=' + that.data.isTab,
              })
            }
          }
        })
      } else {
        wx.showToast({
          title: json.msg,
        })
      }
    })
  },
  /**
   * 微信登陆
   */
  wxLogin: function (e) {
    wx.showLoading({
      title: '登陆中...',
      mask: true,
    })
   
    setTimeout(function () {
      wx.hideLoading()
    }, 8000)
    var that = this;
    wx.login({
      success: function (res) {
        var code = res.code;
        wx.getUserInfo({
          success: function (res) {
            that.startLogin(res, code);
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */

  onLaunch: function () {

  },
  onLoad: function (options) {
    var isTab = false;
    if(options.isTab != undefined){
      isTab = options.isTab;
    }
    this.setData({
      loginType: options.type,
      isTab: isTab
    })
    wx.getSystemInfo({
      success: function (res) {
        if (myUtils.compareVersion(res.SDKVersion,'1.4.4') < 0){
          wx.showModal({
            title: '提示',
            content: '当前微信版本过低，无法正常登陆，请升级到最新微信版本后重试。'
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  /*事件函数*/
  /*登录*/
  onLaunch: function () {
    wx.login({
      success: function (res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            _url: app.globalData._url + '/user/getXcxCode',
            data: {
              code: res.code
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    });
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


  getPhoneNumber: function (e) {

    var that = this.base64_decode11(e.detail.encryptedData);;
  },
  getData: function (params, cb) {
    wx.request({
      url: params._url,
      data: params.data,
      method: 'GET',
      header: {
        'content-type': 'application/json;charset=UTF-8'
      },
      success: function (res) {
        if (res.statusCode == 200) {
          typeof cb === 'function' && cb(res.data)
        }
      },
      fail: function () {
      },
      complete: function () {
      }
    })
  },

  base64_decode11: function (str) {
    var c1, c2, c3, c4;
    var base64DecodeChars = new Array(
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57,
      58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6,
      7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
      25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
      37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1,
      -1, -1
    );
    var i = 0, len = str.length, string = '';

    while (i < len) {
      do {
        c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff]
      } while (
        i < len && c1 == -1
      );

      if (c1 == -1) break;

      do {
        c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff]
      } while (
        i < len && c2 == -1
      );

      if (c2 == -1) break;

      string += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));

      do {
        c3 = str.charCodeAt(i++) & 0xff;
        if (c3 == 61)
          return string;

        c3 = base64DecodeChars[c3]
      } while (
        i < len && c3 == -1
      );

      if (c3 == -1) break;

      string += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));

      do {
        c4 = str.charCodeAt(i++) & 0xff;
        if (c4 == 61) return string;
        c4 = base64DecodeChars[c4]
      } while (
        i < len && c4 == -1
      );

      if (c4 == -1) break;

      string += String.fromCharCode(((c3 & 0x03) << 6) | c4)
    }
    return string;
  }


})